<?php
    App::uses('AppController', 'Controller');
    /**
     * ExpenseGroups Controller
     *
     * @property ExpenseGroup $ExpenseGroup
     * @property PaginatorComponent $Paginator
     */
    class ExpenseGroupsController extends AppController {

        /**
         * Components
         *
         * @var array
         */
        public $components = array('Paginator');

        /**
         * index method
         *
         * @return void
         */
        public function index() {
            $this->layout='list';
            $model = $this->modelClass;
            $this->Filter->addFilters(
                array(
                    'filter1' => array(
                        $model.'.name' => array(
                            'operator' => 'LIKE',
                            'value' => array(
                                'before' => '%', // opcional
                                'after'  => '%'  // opcional
                            )
                        ),
                    ),
                )
            );

            $this->Filter->setPaginate('order', $model.'.id DESC')    ; // opcional
            $this->Filter->setPaginate('limit', 10);              // opcional
            // Define conditions
            $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

            $this->$model->recursive = 0;
            $this->set('expenseGroups', $this->Paginator->paginate(array($model.".token_user"=>$this->tokenUser())));
            $mnCadastro = 'active';
            $mnGroup = 'active';
            $this->set(compact('mnCadastro','mnGroup'));
        }

        /**
         * view method
         *
         * @throws NotFoundException
         * @param string $id
         * @return void
         */
        public function view($id = null) {
            $this->layout='cadastre';
            if (!$this->ExpenseGroup->exists($id)) {
                throw new NotFoundException(__('Invalid expense group'));
            }
            $options = array('conditions' => array('ExpenseGroup.' . $this->ExpenseGroup->primaryKey => $id));
            $this->set('expenseGroup', $this->ExpenseGroup->find('first', $options));
        }

        /**
         * add method
         *
         * @return void
         */
        public function add() {
            $this->layout='cadastre';
            $model = $this->modelClass;
            if ($this->request->is('post')) {
                $this->request->data[$model]['token_user'] = $this->tokenUser();
                $this->ExpenseGroup->create();
                if ($this->ExpenseGroup->save($this->request->data)) {
                    $this->msgSave();
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->msgSave(false);
                }
            }
        }

        /**
         * edit method
         *
         * @throws NotFoundException
         * @param string $id
         * @return void
         */
        public function edit($id = null) {
            $this->layout='cadastre';
            if (!$this->ExpenseGroup->exists($id)) {
                $this->msgNotFould();
                return $this->redirect(array('action' => 'index'));
            }
            if ($this->request->is(array('post', 'put'))) {
                if ($this->ExpenseGroup->save($this->request->data)) {
                    $this->msgEdit();
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->msgEdit(false);
                }
            } else {
                $options = array('conditions' => array('ExpenseGroup.' . $this->ExpenseGroup->primaryKey => $id));
                $this->request->data = $this->ExpenseGroup->find('first', $options);
            }
        }

        /**
         * delete method
         *
         * @throws NotFoundException
         * @param string $id
         * @return void
         */
        public function delete($id = null) {
            $this->ExpenseGroup->id = $id;
            if (!$this->ExpenseGroup->exists()) {
                $this->msgNotFould();
                return $this->redirect(array('action' => 'index'));
            }
            $this->request->allowMethod('post', 'delete');
            if ($this->ExpenseGroup->delete()) {
                $this->msgDelete();
            } else {
                $this->msgDelete(false);
            }
            return $this->redirect(array('action' => 'index'));
        }
    }
